#pragma once
#include "Epoll.h"
#include "Channel.h"
#include <memory>
#include<queue>
#include<mutex>
#include<sys/eventfd.h>

class Epoll;
class Channel;

class EventLoop
{
private:
    // Epoll *ep_;
    std::unique_ptr<Epoll> ep_;
    std::function<void(EventLoop *)> epolltimeoutcallback_;
    pid_t threadid_;    //事件循环所在线程的id
    
    std::queue<std::function<void()>> taskqueue_;
    std::mutex mutex_;

    int wakeupfd_;

    std::unique_ptr<Channel> wakechannel_;
public:
    EventLoop();
    ~EventLoop();

    void run();

    Epoll* ep();


    void updatechannel(Channel *ch);

    void removechannel(Channel *ch);  //从红黑树上删除channel

    void setepolltimeoutcallback(std::function<void(EventLoop *)>);

    bool isinloopthread(); //判断当前线程是否为事件循环线程

    void queueinloop(std::function<void()> f);

    void wakeup();

    void haddlewakeup();

};
